﻿using MediatR;
using ProduceBasicData.Api.Application.Command.Material;
using ProduceBasicData.Domain.Material;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Material
{
    /// <summary>
    /// 物料下拉框命令处理器
    /// </summary>
    public class GetMaterialDropdownCommandHandler : IRequestHandler<GetMaterialDropdownCommand, ApiResult<List<ProductMaterialModel>>>
    {
        private readonly IBaseRespository<ProductMaterialModel> _materialRepo;

        public GetMaterialDropdownCommandHandler(IBaseRespository<ProductMaterialModel> materialRepo)
        {
            _materialRepo = materialRepo;
        }

        public Task<ApiResult<List<ProductMaterialModel>>> Handle(GetMaterialDropdownCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<List<ProductMaterialModel>>()
            {
                Code = ApiEnum.Success,
                Msg = "物料查询成功"
            };
            var materials = _materialRepo.GetAll().Where(x => !x.Isdel).ToList();

            result.Data = materials;

            return Task.FromResult(result);
        }
    }
}